package com.chub.study.modules.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chub.RespResult;
import com.chub.study.modules.constants.enums.CommEnum;
import com.chub.study.modules.constants.enums.TLogEnum;
import com.chub.study.modules.entity.TLog;
import com.chub.study.modules.entity.TMessage;
import com.chub.study.modules.mapper.TLogMapper;
import com.chub.study.modules.service.TLogService;
import com.chub.study.modules.service.TMessageService;
import com.chub.study.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author Chub
 * @since 2022-11-02
 */
@Slf4j
@Service
public class TLogServiceImpl extends ServiceImpl<TLogMapper, TLog> implements TLogService {

    @Autowired
    private TLogMapper logMapper;

    @Resource
    private TMessageService messageService;


    @Override
    public boolean saveHandleLog(TMessage message, String logLevel, String handleState, String info)  {
        TLog tLog = new TLog();
        // tLog.setLogId(IdUtil.simpleUUID());
        tLog.setMsgId(message.getMsgId());
        tLog.setSendChnlCode(message.getSendChnlCode());
        tLog.setSendChnlName(message.getSendChnlName());
        tLog.setSendSrlNum(message.getSendSrlNum());
        tLog.setSendTime(message.getSendTime());
        tLog.setTranCode(message.getTranCode());
        tLog.setTranName(message.getTranName());
        tLog.setLogLevel(logLevel);   // 日志级别(0-debug, 1-info, 2-warn, 3-error)
        tLog.setLogContent(info);

        TMessage tmpMessage = new TMessage();
        tmpMessage.setMsgId(message.getMsgId());
        tmpMessage.setHandleState(handleState);
        tmpMessage.setEffFlag(CommEnum.EFF_FLAG_VALID.getCode());

        // 处理状态(1-接收报文, 2-无效报文, 3-处理失败, 4-处理完成)
        switch (handleState) {
            case "1":
                break;
            case "2":
                tmpMessage.setHandleResult(JSONObject.toJSONString(RespResult.error(info)));
                break;
            case "3":
                tmpMessage.setHandleResult(JSONObject.toJSONString(RespResult.error(Constants.REQ_HANDLE_FAIL)));
                break;
            case "4":
                tmpMessage.setHandleResult(JSONObject.toJSONString(RespResult.success(info)));
                break;
            default:
                return false;

        }
        // logService.saveTLog(tLog, info);
        log.info("日志信息-->{}", JSONObject.toJSONString(tLog));
        this.save(tLog);

        // try {
        //     Thread.sleep(5000);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }

        return messageService.updateById(tmpMessage);
        // return messageService.saveOrUpdate(tmpMessage);
    }

    @Override
    public boolean debug(TMessage message, String handleState, String respInfo) {
        return saveHandleLog(message, TLogEnum.LOG_LEVEL_DEBUG.getCode(), handleState, respInfo);
    }

    @Override
    public boolean info(TMessage message, String handleState, String respInfo) {
        return saveHandleLog(message, TLogEnum.LOG_LEVEL_INFO.getCode(), handleState, respInfo);
    }

    @Override
    public boolean warn(TMessage message, String handleState, String respInfo) {
        return saveHandleLog(message, TLogEnum.LOG_LEVEL_WARN.getCode(), handleState, respInfo);
    }

    @Override
    public boolean error(TMessage message, String handleState, String respInfo) {
        return saveHandleLog(message, TLogEnum.LOG_LEVEL_ERROR.getCode(), handleState, respInfo);
    }


}
